package com.xb.cloud.merchantapi.adapter;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.xb.cloud.gateway.client.Adapter;
import com.xb.cloud.gateway.client.AdapterContext;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.merchantapi.product.service.EndMerchantPassportUserService;
/**
 * 
 * @author ye
 *
 */

@Component
public class EndMerchantApiPassportAdapter extends Adapter{

	@Resource
	EndMerchantPassportUserService endMerchantPassportUserService;

	
	@Override
	public RtnInfo execute(String urlPath, Boolean isLogined, Map<String, String> bizInfo, AdapterContext context) {
		RtnInfo rtnInfo = null;
		long time = System.currentTimeMillis();
		long companyId = context.getCompanyId();
		
		if ("/h5/endMerchantApiPassport/sendSms".equalsIgnoreCase(urlPath)) {
			String phoneNum = bizInfo.get("phoneNum");
			return endMerchantPassportUserService.sendSms(companyId, phoneNum);
		}

		if ("/h5/endMerchantApiPassport/initUser".equalsIgnoreCase(urlPath)) {
			// TODO:用户初始化，一期只简单初始化密码
			String phoneNum = bizInfo.get("phoneNum");
			String verifyCode = bizInfo.get("verifyCode");
			String password = bizInfo.get("password");
			return endMerchantPassportUserService.resetPassword(companyId, phoneNum, verifyCode, password);
		}
		
		if ("/h5/endMerchantApiPassport/verifyCode/login".equalsIgnoreCase(urlPath)) {
			String phoneNum = bizInfo.get("phoneNum");
			String verifyCode = bizInfo.get("verifyCode");
			return endMerchantPassportUserService.loginWithVerifyCode(companyId, phoneNum, verifyCode);
		}
		
		if ("/h5/endMerchantApiPassport/register".equalsIgnoreCase(urlPath)) {
			String phoneNum = bizInfo.get("phoneNum");
			String verifyCode = bizInfo.get("verifyCode");
			String password = bizInfo.get("password");
			return endMerchantPassportUserService.register(companyId, phoneNum, verifyCode, password);
		}
		if ("/h5/endMerchantApiPassport/password/login".equalsIgnoreCase(urlPath)) {
			String phoneNum = bizInfo.get("phoneNum");
			String password = bizInfo.get("password");
			return endMerchantPassportUserService.loginWithPassword(companyId, phoneNum, password);
		}

		if ("/h5/endMerchantApiPassport/autoLogin".equalsIgnoreCase(urlPath)) {
			String autoUserToken = bizInfo.get("autoUserToken");
			return endMerchantPassportUserService.autoLogin(autoUserToken);
		}
				
		if ("/h5/endMerchantApiPassport/logout".equalsIgnoreCase(urlPath)) {
			String userToken = context.getRequestInfo().getMerchantUserToken();
			String autoUserToken = bizInfo.get("autoUserToken");
			return endMerchantPassportUserService.logout(userToken, autoUserToken);
		}

		if ("/h5/endMerchantApiPassport/resetPassword".equalsIgnoreCase(urlPath)) {
			String phoneNum = bizInfo.get("phoneNum");
			String verifyCode = bizInfo.get("verifyCode");
			String password = bizInfo.get("password");
			return endMerchantPassportUserService.resetPassword(companyId, phoneNum, verifyCode, password);
		}
		
		
		System.out.println(System.currentTimeMillis() - time);
		return rtnInfo;
	}

	@Override
	public String getUrlMappingPath() {
		return "/h5/endMerchantApiPassport/**";
	}
	
	
	
	
}
